package com.ly.hotel.view.common;

import com.ly.hotel.R;

import android.content.Context;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

//自定义ImageButton，模拟ImageButton，并在其下方显示文字
//提供Button的部分接口
public class ImgTxtButton extends LinearLayout {
	public float textSize = 18;
	public int wid;
	public int pd;
	public int imgPd;

	public ImgTxtButton(Context context, int imageResId, String text, float textSize) {

		super(context);
		this.textSize = textSize;
		wid = context.getResources().getDimensionPixelSize(R.dimen.px120);
		pd = context.getResources().getDimensionPixelSize(R.dimen.px54);
		imgPd = context.getResources().getDimensionPixelSize(R.dimen.px9);
		initView(context, imageResId, text);
	}
	
	public void initView(Context context, int imageResId, String text){
		mButtonImage = new ImageView(context);

		mButtonText = new TextView(context);

		setImageResource(imageResId);
     
		LayoutParams layoutParams = new LayoutParams(wid,wid);
		layoutParams.setMargins(pd,0,pd,0);
		LayoutParams tLP = new LayoutParams(LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		mButtonImage.setLayoutParams(layoutParams);
		mButtonImage.setScaleType(ImageView.ScaleType.FIT_XY);
		mButtonImage.setPadding(imgPd, imgPd, imgPd, imgPd);

		setText(text);
		setTextColor(0xFFFFFFFF);

		mButtonImage.setPadding(imgPd, 0, imgPd, imgPd);
		mButtonText.setLayoutParams(tLP);
		mButtonText.setGravity(Gravity.CENTER);
		mButtonText.setSingleLine(true);
		mButtonText.setTextSize(TypedValue.COMPLEX_UNIT_PX,textSize);
		// 设置本布局的属性
		setClickable(true); // 可点击
		setFocusable(true); // 可聚焦
		setOrientation(LinearLayout.VERTICAL); // 垂直布局

		// 首先添加Image，然后才添加Text

		// 添加顺序将会影响布局效果
		addView(mButtonImage);
		addView(mButtonText);
	}
	// ----------------public method-----------------------------

	/*
	 * setImageResource方法
	 */

	public void setImageResource(int resId) {

		mButtonImage.setImageResource(resId);
	}

	/*
	 * setText方法
	 */

	public void setText(int resId) {

		mButtonText.setText(resId);
	}

	public void setText(CharSequence buttonText) {

		mButtonText.setText(buttonText);
	}

	/*
	 * setTextColor方法
	 */

	public void setTextColor(int color) {

		mButtonText.setTextColor(color);
	}

	// ----------------private attribute-----------------------------

	private ImageView mButtonImage = null;

	private TextView mButtonText = null;
}
